package com.tz.farmer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.model.FarmerProcess;
import com.tz.farmer.model.FarmerSummary;
import com.tz.farmer.service.IFarmerProcessService;
import com.tz.farmer.service.IFarmerSummaryService;
import com.tz.farmer.service.IFarmerSunDataService;
import com.tz.farmer.service.ISummaryService;
import com.tz.farmer.so.DefaultSo;
import com.tz.farmer.vo.SummaryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/cultivate")
@Api(value = "阳光渔场", tags = "阳光渔场-养殖接口")
public class CultivateController {

    private static final String dataKey = "cultivateTotalTask";
    private static final String market = "阳光渔场-养殖";
    private static final String channel = "抽检";

    @Autowired
    private IFarmerProcessService processService;

    @Autowired
    private IFarmerSummaryService farmerSummaryService;

    @Autowired
    private IFarmerSunDataService dataService;

    @Autowired
    private ISummaryService summaryService;

    @GetMapping("/summary")
    @ApiOperation(value = "渔场养殖-汇总", response = SummaryVo.class)
    public ResultEx summary() {
        SummaryVo response = summaryService.getSummary(market, channel, dataKey);

        return ResultEx.builder().makeSuccessResult(response);
    }

    @PostMapping("/set")
    @ApiOperation(value = "设置总任务")
    public ResultEx setTotalTask(@RequestBody DefaultSo body) {
        if (ObjectUtils.isEmpty(body.getCount())) {
            return ResultEx.builder().makeFailedResult("缺少任务总数");
        }

        if (dataService.setData(market, dataKey, body.getCount())) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }

    @PostMapping("/list")
    @ApiOperation(value = "农事操作列表")
    public ResultEx list(@RequestBody @Validated DefaultSo body) {
        IPage<FarmerProcess> res = processService.selectPage(body, market);

        return ResultEx.builder().makeSuccessResult(res);
    }

    @PostMapping("/add-check")
    @ApiOperation(value = "添加抽检信息")
    public ResultEx addSummary(@RequestBody @Validated FarmerSummary body) {
        if (body != null) body.setChannel(channel);
        if (body != null) body.setMarket(market);
        // 验证是否更新成功
        if (farmerSummaryService.saveOrUpdate(body)) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }

    @PostMapping("/add-process")
    @ApiOperation(value = "添加农事操作")
    public ResultEx addProcess(@RequestBody @Validated FarmerProcess body) {
        if (body != null) body.setMarket(market);
        // 验证是否更新成功
        if (processService.saveOrUpdate(body)) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }
}

